<?
/**
 * Google RSS News Plugin for Gnuboard4
 *
 * Copyright (c) 2008 Choi Jae-Young <www.miwit.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

include_once("_common.php");
@include_once("$g4[path]/lib/mw.cache.lib.php");

header("Content-Type: text/html; charset=$g4[charset]");

if (!function_exists("unhtmlspecialchars")) {
    function unhtmlspecialchars($str) // htmlspecialchars 반대 
    {
	$trans = get_html_translation_table();
	$trans = array_flip($trans);
	$str = strtr($str, $trans);
	return $str;
    }
}

function mw_get_google_rss_news($topic, $cache_time=0)
{
    global $g4;
    $url = "http://news.google.co.kr/nwshp?oe=UTF-8&tab=wn&ned=kr&output=rss&topic=$topic";

    $charset = str_replace("-", "", $g4[charset]);
    $charset = strtolower($charset);

    $cache_file = "$g4[path]/data/mw.cache/google-news-{$topic}";
    if (!$topic)
        $cache_file = "$g4[path]/data/mw.cache/google-news-po";
    $xml = @mw_cache_read($cache_file, $cache_time);

    if (!$xml) {
	$fp = fsockopen ("news.google.co.kr", 80, $errno, $errstr, 30);
	if (!$fp)
	    return false;
	fputs($fp, "GET /nwshp?ned=kr&output=rss&topic=$topic HTTP/1.0\r\n");
	fputs($fp, "Host: news.google.co.kr:80\r\n");
	fputs($fp, "\r\n");
	while (trim($buffer = fgets($fp,1024)) != "") $header .= $buffer;
	while (!feof($fp)) $buffer .= fgets($fp,1024);
	fclose($fp);

	$xml = trim($buffer);
	//if ($charset == "euckr") $xml = iconv("utf-8", "euckr", $xml);
	@mw_cache_write($cache_file, $xml);
    }

    $item = array();
    preg_match_all("/<item>(.*)<\/item>/iUs", $xml, $matchs);
    for ($i=0, $max=count($matchs[1]); $i<$max; $i++) {

	if ($charset == "euckr")
	    $matchs[1][$i] = iconv("utf-8", "euckr", $matchs[1][$i]);
	preg_match("/<title>(.*)<\/title>/iUs", $matchs[1][$i], $match);
	$item[$i][title] = trim($match[1]);
	$item[$i][title] = str_replace("&apos;", "'", $item[$i][title]);
	preg_match("/<link>(.*)<\/link>/iUs", $matchs[1][$i], $match);
	$item[$i][link] = trim($match[1]);	
	preg_match("/img src=(.*) /iUs", $matchs[1][$i], $match);
	$item[$i][img] = trim($match[1]);
	$item[$i][img] = preg_replace("/&quot;/", "", $item[$i][img]);
	$item[$i][img] = preg_replace("/\/\//", "", $item[$i][img]);
	$item[$i][img] = set_http($item[$i][img]);
    }
    return $item;
}

$topics = array('', 'p', 'y', 'l', 'w', 't', 's', 'e');
if (!in_array($topic, $topics)) exit;

$item = mw_get_google_rss_news($topic, 60);
$item_count = count($item);
$item_count = 7;

$img_flag = false;

echo "<ul>\n";

for ($i=0; $i<$item_count; $i++) {
    if (trim($item[$i][img])) {
        $r = $i;
        break;
    }
}

echo "<li class='image'><a href=\"{$item[$r][link]}\" target=\"_blank\"><img src=\"".urldecode($item[$r][img])."\"></a></li>";

for ($i=0; $i<$item_count; $i++) {
    echo "<li><a href=\"{$item[$i][link]}\" target=\"_blank\">{$item[$i][title]}</a></li>\n";
}
echo "</ul>\n";

?>
